{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e6c1f318",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda3\\lib\\site-packages\\gensim\\similarities\\__init__.py:15: UserWarning: The gensim.similarities.levenshtein submodule is disabled, because the optional Levenshtein package <https://pypi.org/project/python-Levenshtein/> is unavailable. Install Levenhstein (e.g. `pip install python-Levenshtein`) to suppress this warning.\n",
      "  warnings.warn(msg)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import xgboost as xgb\n",
    "import lightgbm as lgb\n",
    "from gensim.models import Word2Vec\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ad78a16f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">d2gkma_ a.1.1.1 (A:) Protozoan/bacterial hemoglobin {Mycobacterium tuberculosis, HbN [TaxId: 1773]}\n",
      "gllsrlrkrepisiydkiggheaievvvedffvrvladdqlsaffsgtnmsrlkgkqvef\n",
      "faaalggpepytgapmkqvhqgrgitmhhfslvaghladaltaagvpsetiteilgviap\n",
      "lavdvts\n",
      ">d1ngka_ a.1.1.1 (A:) Protozoan/bacterial hemoglobin {Mycobacterium tuberculosis, HbO [TaxId: 1773]}\n",
      "ksfydavggaktfdaivsrfyaqvaedevlrrvypeddlagaeerlrmfleqywggprty\n",
      "seqrghprlrmrhapfrislierdawlrcmhtavasidsetlddehrrelldylemaahs\n",
      "lvnspf\n",
      ">d2bkma_ a.1.1.1 (A:) automated matches {Geobacillus stearothermophilus [TaxId: 1422]}\n",
      "eqwqtlyeaiggeetvaklveafyrrvaahpdlrpifpddltetahkqkqfl\n"
     ]
    }
   ],
   "source": [
    "with open(\"训练集/astral_train.fa\", \"r\") as f:  # 打开文件\n",
    "    data = f.read()  # 读取文件\n",
    "print(data[0:600])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1773a404",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取蛋白质的字母序列\n",
    "l1 = [i.split('}')[1].replace('\\n','') for i in data.split('>')[1:]]\n",
    "# 读取蛋白质的字母id\n",
    "l2 = [i.split(' ')[1].split('.')[0] for i in data.split('>')[1:]]\n",
    "# 读取蛋白质的数字id\n",
    "l3 = [i.split(' ')[1].split('.')[1] for i in data.split('>')[1:]]\n",
    "# 组合成dataframe\n",
    "df = pd.DataFrame({'words':l1,'id1':l2,'id2':l3})\n",
    "df = df.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "323750fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"测试集/astral_test.fa\", \"r\") as f:  # 打开文件\n",
    "    data = f.read()  # 读取文件\n",
    "l1 = [''.join(i.split('\\n')[1:]) for i in data.split('>')[1:]]\n",
    "l2 = [i.split('\\n')[0] for i in data.split('>')[1:]]\n",
    "df2 = pd.DataFrame({'index':l2,'words':l1})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "0cace43b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 记录蛋白质字母id和数字id的组合\n",
    "df['id3'] = df['id1']+'.'+df['id2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ef27c46d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将训练集和测试集合并\n",
    "df = pd.concat([df,df2])\n",
    "# 求字母序列长度\n",
    "df['len'] = df['words'].apply(lambda x:len(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5c43e704",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'g': 156660, 'l': 202701, 's': 129068, 'r': 111412, 'k': 126220, 'e': 148421, 'p': 98226, 'i': 126880, 'y': 74309, 'd': 127283, 'h': 49943, 'a': 177965, 'v': 155471, 'f': 87382, 'q': 79715, 't': 116158, 'n': 90649, 'm': 47411, 'w': 28814, 'c': 28809, 'X': 285, 'x': 39, 'z': 1}\n"
     ]
    }
   ],
   "source": [
    "# 记录字母序列中出现过哪些字母\n",
    "l = []\n",
    "for i in df['words'].apply(list).values.tolist():\n",
    "    for j in i:\n",
    "        l.append(j)\n",
    "from collections import Counter\n",
    "c = Counter(l)\n",
    "print (dict(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "493a7507",
   "metadata": {},
   "outputs": [],
   "source": [
    "def w2v_feat(df, tid, fea):\n",
    "    data_frame=df.copy()\n",
    "    \n",
    "    # 将每个人的单词组成一个句子\n",
    "    df_sens = df['words'].apply(list).values.tolist()\n",
    "    print('共有',len(df_sens),'人')\n",
    "    \n",
    "    # 训练，window=4每个词语周边搜索窗口为4, min_count=2忽略出现次数少于2次的词语,vector_size=10最后产生10维向量\n",
    "    model = Word2Vec(df_sens, window=4, min_count=1,vector_size=10)\n",
    "    for i in dict(c):\n",
    "        print(i,'对应的词向量是',model.wv[i])\n",
    "    \n",
    "    tmp = []\n",
    "    for i in dict(c):\n",
    "        tmp.append(([i]+model.wv[i].tolist()))\n",
    "    tmp_df = pd.DataFrame(tmp) # 将每个词的词向量构造成dataframe\n",
    "    w2c_list = [f'w2c_{fea}_{n}' for n in range(10)]\n",
    "    tmp_df.columns = [fea] + w2c_list # 修改列名\n",
    "    print('tmp_df',tmp_df.head(3))\n",
    "    \n",
    "    # 将df改造一下方便添加特征。\n",
    "    df['index2']=df.apply(lambda x:[str(x['index']) for i in range(x.len)],axis=1)\n",
    "    index0 = []\n",
    "    for i in df.index2.values.tolist():\n",
    "        for j in i:\n",
    "            index0.append(j)\n",
    "    # 将词向量和每个人的特征merge一下\n",
    "    df2 = pd.DataFrame({'index':index0,'words':list(''.join(df['words'].values))})\n",
    "    tmp_df = df2[[tid, fea]].merge(tmp_df, how='left',on=fea)\n",
    "\n",
    "    # 统计词向量的统计特征\n",
    "    stat_list = ['mean', 'std','sum']\n",
    "    # 去除每个人重复词向量，然后计算统计特征\n",
    "    tmp_df = tmp_df.drop_duplicates().groupby(tid).agg(stat_list).reset_index()\n",
    "    tmp_df.columns = [tid] + [f'{p}_{q}' for p in w2c_list for q in stat_list]\n",
    "\n",
    "    return tmp_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d1b6c668",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "共有 11843 人\n",
      "g 对应的词向量是 [ 0.12083764  0.45871532  0.1438399   0.2627601   0.32339013  0.09466244\n",
      "  0.8330164   0.16644207  0.45228928 -0.05475282]\n",
      "l 对应的词向量是 [-0.77510786 -0.26827797  0.2017123  -0.6050412  -0.09990497 -0.3503139\n",
      "  0.6178934   0.10484625 -0.16002762 -0.3607721 ]\n",
      "s 对应的词向量是 [ 0.15043202  0.13437702  0.20532253  0.26239255 -0.18978935  0.40872872\n",
      "  0.6727631  -0.23502304 -0.14302571  0.62639934]\n",
      "r 对应的词向量是 [-1.1152759  -0.14926873 -0.15826961  0.28426906  0.2963757  -0.14924902\n",
      "  0.6251291   0.62675256  0.64943653 -0.43715522]\n",
      "k 对应的词向量是 [ 0.74469924  0.02626805 -0.27263036 -0.7353789  -0.15377544  0.43994942\n",
      " -0.32847148  0.7608811  -0.5472175   1.0457903 ]\n",
      "e 对应的词向量是 [-0.25958318  0.01964059 -0.6957751  -0.02481686  0.3196474   0.28280205\n",
      "  0.03030771  0.7648866  -0.41090617  0.22621788]\n",
      "p 对应的词向量是 [-0.30961025 -0.255337   -0.17484339  0.50669277  0.3363199  -0.21364082\n",
      "  0.8276381  -0.01026826  0.27193126 -0.01848196]\n",
      "i 对应的词向量是 [ 0.17715262  0.23424366  0.40480405 -1.1400336  -0.6348907  -0.09612466\n",
      "  0.17113194  0.27415904 -0.30446076  0.6736865 ]\n",
      "y 对应的词向量是 [ 0.54585826 -0.47297603 -0.31890714 -0.6088791  -0.6989531   0.09165424\n",
      "  0.5645154  -0.02786777 -0.01858127  0.36244035]\n",
      "d 对应的词向量是 [ 0.36839122  0.38333446 -0.19322258  0.30873203  0.48553    -0.04134572\n",
      "  0.44477642  0.38086784 -0.26556793  0.2965978 ]\n",
      "h 对应的词向量是 [-0.2814502  -0.39286682 -0.22064315  0.27889606  0.64114594 -0.494898\n",
      "  0.68200773 -0.15995783  0.8131615   0.637436  ]\n",
      "a 对应的词向量是 [-0.38535327  0.00776418  0.92643785  0.10877167  0.6450131   0.07367364\n",
      "  0.89262867  0.13315684  0.28847736 -0.75093937]\n",
      "v 对应的词向量是 [-0.39457905  0.18491252  0.46313784 -0.5022163  -0.07883795 -0.11244297\n",
      "  0.71152425  0.26708934  0.24358393 -0.4179002 ]\n",
      "f 对应的词向量是 [ 0.33019105 -0.17964096 -0.2823304  -0.63504314 -0.64253527  0.09217942\n",
      "  0.44820273  0.14515194  0.12098969  0.5118811 ]\n",
      "q 对应的词向量是 [-0.3572972  -0.7079418   0.17085922  0.16591214  0.17436829  0.28847352\n",
      "  0.5378091  -0.23383355 -0.4601206   0.19290417]\n",
      "t 对应的词向量是 [ 0.35427898  0.13981506  0.48181656 -0.09052865 -0.22499953 -0.09907121\n",
      "  0.7280871  -0.01583236  0.09636171  0.36280406]\n",
      "n 对应的词向量是 [ 1.2277964  -0.21993768 -0.02502299 -0.11941814 -0.1158604   0.3112471\n",
      "  0.09372685  0.2302822  -0.4371936   1.2043011 ]\n",
      "m 对应的词向量是 [ 0.09439707 -0.34794536  0.26309937 -0.5487558   0.02136996  0.18183532\n",
      "  0.35180327  0.3044239   0.33011293  0.20711757]\n",
      "w 对应的词向量是 [ 0.05498965 -0.86724323 -0.60798883 -0.46356535 -0.41815937  0.10588362\n",
      "  1.1515709  -0.41051054  0.8838596  -0.44498667]\n",
      "c 对应的词向量是 [-0.52611184  0.3935374   0.06970662 -0.74064434  0.675794    0.29323173\n",
      "  0.80942076 -1.7155575   0.5678222   1.6751763 ]\n",
      "X 对应的词向量是 [ 0.09808534 -0.13228603  0.2914806  -0.02098102  0.19847167  0.04892538\n",
      "  0.302744    0.517382   -0.37475103  0.08233383]\n",
      "x 对应的词向量是 [-0.2615464   0.0064311   0.14610627 -0.11606103  0.36193755 -0.08048397\n",
      "  0.46168444  0.52988756 -0.35034832  0.08976518]\n",
      "z 对应的词向量是 [-0.07967395 -0.06990711 -0.04334643 -0.10869014  0.10034356 -0.0314549\n",
      "  0.1644609   0.09096145 -0.08062878  0.07258534]\n",
      "tmp_df   words  w2c_words_0  w2c_words_1  w2c_words_2  w2c_words_3  w2c_words_4  \\\n",
      "0     g     0.120838     0.458715     0.143840     0.262760     0.323390   \n",
      "1     l    -0.775108    -0.268278     0.201712    -0.605041    -0.099905   \n",
      "2     s     0.150432     0.134377     0.205323     0.262393    -0.189789   \n",
      "\n",
      "   w2c_words_5  w2c_words_6  w2c_words_7  w2c_words_8  w2c_words_9  \n",
      "0     0.094662     0.833016     0.166442     0.452289    -0.054753  \n",
      "1    -0.350314     0.617893     0.104846    -0.160028    -0.360772  \n",
      "2     0.408729     0.672763    -0.235023    -0.143026     0.626399  \n"
     ]
    }
   ],
   "source": [
    "train = w2v_feat(df,'index','words')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "89ebf21f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train['index'] = train['index'].astype('str')\n",
    "df['index'] = df['index'].astype('str')\n",
    "train = train.merge(df[['index','len','id1','id2','id3']],how='left',on=['index'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d6ce3ebc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>w2c_words_0_mean</th>\n",
       "      <th>w2c_words_0_std</th>\n",
       "      <th>w2c_words_0_sum</th>\n",
       "      <th>w2c_words_1_mean</th>\n",
       "      <th>w2c_words_1_std</th>\n",
       "      <th>w2c_words_1_sum</th>\n",
       "      <th>w2c_words_2_mean</th>\n",
       "      <th>w2c_words_2_std</th>\n",
       "      <th>w2c_words_2_sum</th>\n",
       "      <th>...</th>\n",
       "      <th>w2c_words_8_mean</th>\n",
       "      <th>w2c_words_8_std</th>\n",
       "      <th>w2c_words_8_sum</th>\n",
       "      <th>w2c_words_9_mean</th>\n",
       "      <th>w2c_words_9_std</th>\n",
       "      <th>w2c_words_9_sum</th>\n",
       "      <th>len</th>\n",
       "      <th>id1</th>\n",
       "      <th>id2</th>\n",
       "      <th>id3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.013099</td>\n",
       "      <td>0.559603</td>\n",
       "      <td>0.235778</td>\n",
       "      <td>-0.078062</td>\n",
       "      <td>0.308304</td>\n",
       "      <td>-1.405121</td>\n",
       "      <td>0.051077</td>\n",
       "      <td>0.383934</td>\n",
       "      <td>0.919385</td>\n",
       "      <td>...</td>\n",
       "      <td>0.028847</td>\n",
       "      <td>0.398730</td>\n",
       "      <td>0.519243</td>\n",
       "      <td>0.239310</td>\n",
       "      <td>0.518660</td>\n",
       "      <td>4.307575</td>\n",
       "      <td>127</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>a.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>-0.011767</td>\n",
       "      <td>0.543080</td>\n",
       "      <td>-0.235345</td>\n",
       "      <td>-0.093941</td>\n",
       "      <td>0.359535</td>\n",
       "      <td>-1.878827</td>\n",
       "      <td>0.019055</td>\n",
       "      <td>0.392033</td>\n",
       "      <td>0.381103</td>\n",
       "      <td>...</td>\n",
       "      <td>0.098546</td>\n",
       "      <td>0.436923</td>\n",
       "      <td>1.970925</td>\n",
       "      <td>0.276888</td>\n",
       "      <td>0.610215</td>\n",
       "      <td>5.537764</td>\n",
       "      <td>126</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>a.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>-0.011767</td>\n",
       "      <td>0.543080</td>\n",
       "      <td>-0.235345</td>\n",
       "      <td>-0.093941</td>\n",
       "      <td>0.359535</td>\n",
       "      <td>-1.878827</td>\n",
       "      <td>0.019055</td>\n",
       "      <td>0.392033</td>\n",
       "      <td>0.381103</td>\n",
       "      <td>...</td>\n",
       "      <td>0.098546</td>\n",
       "      <td>0.436923</td>\n",
       "      <td>1.970925</td>\n",
       "      <td>0.276888</td>\n",
       "      <td>0.610215</td>\n",
       "      <td>5.537764</td>\n",
       "      <td>142</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>a.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>100</td>\n",
       "      <td>-0.011767</td>\n",
       "      <td>0.543080</td>\n",
       "      <td>-0.235345</td>\n",
       "      <td>-0.093941</td>\n",
       "      <td>0.359535</td>\n",
       "      <td>-1.878827</td>\n",
       "      <td>0.019055</td>\n",
       "      <td>0.392033</td>\n",
       "      <td>0.381103</td>\n",
       "      <td>...</td>\n",
       "      <td>0.098546</td>\n",
       "      <td>0.436923</td>\n",
       "      <td>1.970925</td>\n",
       "      <td>0.276888</td>\n",
       "      <td>0.610215</td>\n",
       "      <td>5.537764</td>\n",
       "      <td>82</td>\n",
       "      <td>a</td>\n",
       "      <td>3</td>\n",
       "      <td>a.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1000</td>\n",
       "      <td>-0.015281</td>\n",
       "      <td>0.557728</td>\n",
       "      <td>-0.290334</td>\n",
       "      <td>-0.053241</td>\n",
       "      <td>0.318554</td>\n",
       "      <td>-1.011584</td>\n",
       "      <td>0.052057</td>\n",
       "      <td>0.373142</td>\n",
       "      <td>0.989092</td>\n",
       "      <td>...</td>\n",
       "      <td>0.057214</td>\n",
       "      <td>0.406746</td>\n",
       "      <td>1.087065</td>\n",
       "      <td>0.314882</td>\n",
       "      <td>0.602142</td>\n",
       "      <td>5.982751</td>\n",
       "      <td>78</td>\n",
       "      <td>a</td>\n",
       "      <td>60</td>\n",
       "      <td>a.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11838</th>\n",
       "      <td>g1gk9.1</td>\n",
       "      <td>0.019443</td>\n",
       "      <td>0.529738</td>\n",
       "      <td>0.388853</td>\n",
       "      <td>-0.120233</td>\n",
       "      <td>0.340746</td>\n",
       "      <td>-2.404651</td>\n",
       "      <td>0.030144</td>\n",
       "      <td>0.396650</td>\n",
       "      <td>0.602877</td>\n",
       "      <td>...</td>\n",
       "      <td>0.051418</td>\n",
       "      <td>0.434469</td>\n",
       "      <td>1.028352</td>\n",
       "      <td>0.197246</td>\n",
       "      <td>0.514560</td>\n",
       "      <td>3.944922</td>\n",
       "      <td>766</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11839</th>\n",
       "      <td>g1jjo.1</td>\n",
       "      <td>-0.006536</td>\n",
       "      <td>0.529872</td>\n",
       "      <td>-0.137259</td>\n",
       "      <td>-0.095767</td>\n",
       "      <td>0.350531</td>\n",
       "      <td>-2.011113</td>\n",
       "      <td>0.032028</td>\n",
       "      <td>0.386703</td>\n",
       "      <td>0.672583</td>\n",
       "      <td>...</td>\n",
       "      <td>0.076008</td>\n",
       "      <td>0.438206</td>\n",
       "      <td>1.596174</td>\n",
       "      <td>0.267624</td>\n",
       "      <td>0.596277</td>\n",
       "      <td>5.620098</td>\n",
       "      <td>336</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11840</th>\n",
       "      <td>g1k2x.1</td>\n",
       "      <td>-0.006536</td>\n",
       "      <td>0.529872</td>\n",
       "      <td>-0.137259</td>\n",
       "      <td>-0.095767</td>\n",
       "      <td>0.350531</td>\n",
       "      <td>-2.011113</td>\n",
       "      <td>0.032028</td>\n",
       "      <td>0.386703</td>\n",
       "      <td>0.672583</td>\n",
       "      <td>...</td>\n",
       "      <td>0.076008</td>\n",
       "      <td>0.438206</td>\n",
       "      <td>1.596174</td>\n",
       "      <td>0.267624</td>\n",
       "      <td>0.596277</td>\n",
       "      <td>5.620098</td>\n",
       "      <td>292</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11841</th>\n",
       "      <td>g1o7</td>\n",
       "      <td>-0.015721</td>\n",
       "      <td>0.541919</td>\n",
       "      <td>-0.314412</td>\n",
       "      <td>-0.112268</td>\n",
       "      <td>0.351170</td>\n",
       "      <td>-2.245357</td>\n",
       "      <td>0.013389</td>\n",
       "      <td>0.386950</td>\n",
       "      <td>0.267779</td>\n",
       "      <td>...</td>\n",
       "      <td>0.095032</td>\n",
       "      <td>0.440603</td>\n",
       "      <td>1.900635</td>\n",
       "      <td>0.247321</td>\n",
       "      <td>0.604274</td>\n",
       "      <td>4.946411</td>\n",
       "      <td>96</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11842</th>\n",
       "      <td>g1ugw.1</td>\n",
       "      <td>0.019443</td>\n",
       "      <td>0.529738</td>\n",
       "      <td>0.388853</td>\n",
       "      <td>-0.120233</td>\n",
       "      <td>0.340746</td>\n",
       "      <td>-2.404651</td>\n",
       "      <td>0.030144</td>\n",
       "      <td>0.396650</td>\n",
       "      <td>0.602877</td>\n",
       "      <td>...</td>\n",
       "      <td>0.051418</td>\n",
       "      <td>0.434469</td>\n",
       "      <td>1.028352</td>\n",
       "      <td>0.197246</td>\n",
       "      <td>0.514560</td>\n",
       "      <td>3.944922</td>\n",
       "      <td>150</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>11843 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          index  w2c_words_0_mean  w2c_words_0_std  w2c_words_0_sum  \\\n",
       "0             0          0.013099         0.559603         0.235778   \n",
       "1             1         -0.011767         0.543080        -0.235345   \n",
       "2            10         -0.011767         0.543080        -0.235345   \n",
       "3           100         -0.011767         0.543080        -0.235345   \n",
       "4          1000         -0.015281         0.557728        -0.290334   \n",
       "...         ...               ...              ...              ...   \n",
       "11838  g1gk9.1           0.019443         0.529738         0.388853   \n",
       "11839  g1jjo.1          -0.006536         0.529872        -0.137259   \n",
       "11840  g1k2x.1          -0.006536         0.529872        -0.137259   \n",
       "11841      g1o7         -0.015721         0.541919        -0.314412   \n",
       "11842  g1ugw.1           0.019443         0.529738         0.388853   \n",
       "\n",
       "       w2c_words_1_mean  w2c_words_1_std  w2c_words_1_sum  w2c_words_2_mean  \\\n",
       "0             -0.078062         0.308304        -1.405121          0.051077   \n",
       "1             -0.093941         0.359535        -1.878827          0.019055   \n",
       "2             -0.093941         0.359535        -1.878827          0.019055   \n",
       "3             -0.093941         0.359535        -1.878827          0.019055   \n",
       "4             -0.053241         0.318554        -1.011584          0.052057   \n",
       "...                 ...              ...              ...               ...   \n",
       "11838         -0.120233         0.340746        -2.404651          0.030144   \n",
       "11839         -0.095767         0.350531        -2.011113          0.032028   \n",
       "11840         -0.095767         0.350531        -2.011113          0.032028   \n",
       "11841         -0.112268         0.351170        -2.245357          0.013389   \n",
       "11842         -0.120233         0.340746        -2.404651          0.030144   \n",
       "\n",
       "       w2c_words_2_std  w2c_words_2_sum  ...  w2c_words_8_mean  \\\n",
       "0             0.383934         0.919385  ...          0.028847   \n",
       "1             0.392033         0.381103  ...          0.098546   \n",
       "2             0.392033         0.381103  ...          0.098546   \n",
       "3             0.392033         0.381103  ...          0.098546   \n",
       "4             0.373142         0.989092  ...          0.057214   \n",
       "...                ...              ...  ...               ...   \n",
       "11838         0.396650         0.602877  ...          0.051418   \n",
       "11839         0.386703         0.672583  ...          0.076008   \n",
       "11840         0.386703         0.672583  ...          0.076008   \n",
       "11841         0.386950         0.267779  ...          0.095032   \n",
       "11842         0.396650         0.602877  ...          0.051418   \n",
       "\n",
       "       w2c_words_8_std  w2c_words_8_sum  w2c_words_9_mean  w2c_words_9_std  \\\n",
       "0             0.398730         0.519243          0.239310         0.518660   \n",
       "1             0.436923         1.970925          0.276888         0.610215   \n",
       "2             0.436923         1.970925          0.276888         0.610215   \n",
       "3             0.436923         1.970925          0.276888         0.610215   \n",
       "4             0.406746         1.087065          0.314882         0.602142   \n",
       "...                ...              ...               ...              ...   \n",
       "11838         0.434469         1.028352          0.197246         0.514560   \n",
       "11839         0.438206         1.596174          0.267624         0.596277   \n",
       "11840         0.438206         1.596174          0.267624         0.596277   \n",
       "11841         0.440603         1.900635          0.247321         0.604274   \n",
       "11842         0.434469         1.028352          0.197246         0.514560   \n",
       "\n",
       "       w2c_words_9_sum  len  id1  id2   id3  \n",
       "0             4.307575  127    a    1   a.1  \n",
       "1             5.537764  126    a    1   a.1  \n",
       "2             5.537764  142    a    1   a.1  \n",
       "3             5.537764   82    a    3   a.3  \n",
       "4             5.982751   78    a   60  a.60  \n",
       "...                ...  ...  ...  ...   ...  \n",
       "11838         3.944922  766  NaN  NaN   NaN  \n",
       "11839         5.620098  336  NaN  NaN   NaN  \n",
       "11840         5.620098  292  NaN  NaN   NaN  \n",
       "11841         4.946411   96  NaN  NaN   NaN  \n",
       "11842         3.944922  150  NaN  NaN   NaN  \n",
       "\n",
       "[11843 rows x 35 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a6c1036e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>w2c_words_0_mean</th>\n",
       "      <th>w2c_words_0_std</th>\n",
       "      <th>w2c_words_0_sum</th>\n",
       "      <th>w2c_words_1_mean</th>\n",
       "      <th>w2c_words_1_std</th>\n",
       "      <th>w2c_words_1_sum</th>\n",
       "      <th>w2c_words_2_mean</th>\n",
       "      <th>w2c_words_2_std</th>\n",
       "      <th>w2c_words_2_sum</th>\n",
       "      <th>...</th>\n",
       "      <th>w2c_words_8_mean</th>\n",
       "      <th>w2c_words_8_std</th>\n",
       "      <th>w2c_words_8_sum</th>\n",
       "      <th>w2c_words_9_mean</th>\n",
       "      <th>w2c_words_9_std</th>\n",
       "      <th>w2c_words_9_sum</th>\n",
       "      <th>len</th>\n",
       "      <th>id1</th>\n",
       "      <th>id2</th>\n",
       "      <th>id3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>9472</th>\n",
       "      <td>d1a1va1</td>\n",
       "      <td>-0.015281</td>\n",
       "      <td>0.557728</td>\n",
       "      <td>-0.290334</td>\n",
       "      <td>-0.053241</td>\n",
       "      <td>0.318554</td>\n",
       "      <td>-1.011584</td>\n",
       "      <td>0.052057</td>\n",
       "      <td>0.373142</td>\n",
       "      <td>0.989092</td>\n",
       "      <td>...</td>\n",
       "      <td>0.057214</td>\n",
       "      <td>0.406746</td>\n",
       "      <td>1.087065</td>\n",
       "      <td>0.314882</td>\n",
       "      <td>0.602142</td>\n",
       "      <td>5.982751</td>\n",
       "      <td>136</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9473</th>\n",
       "      <td>d1a5ea_</td>\n",
       "      <td>-0.051581</td>\n",
       "      <td>0.527122</td>\n",
       "      <td>-0.980044</td>\n",
       "      <td>-0.100268</td>\n",
       "      <td>0.368242</td>\n",
       "      <td>-1.905095</td>\n",
       "      <td>0.034407</td>\n",
       "      <td>0.396551</td>\n",
       "      <td>0.653733</td>\n",
       "      <td>...</td>\n",
       "      <td>0.132534</td>\n",
       "      <td>0.420858</td>\n",
       "      <td>2.518142</td>\n",
       "      <td>0.236420</td>\n",
       "      <td>0.598728</td>\n",
       "      <td>4.491974</td>\n",
       "      <td>156</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9474</th>\n",
       "      <td>d1a62a1</td>\n",
       "      <td>-0.018240</td>\n",
       "      <td>0.560308</td>\n",
       "      <td>-0.310081</td>\n",
       "      <td>-0.054832</td>\n",
       "      <td>0.301117</td>\n",
       "      <td>-0.932145</td>\n",
       "      <td>0.072841</td>\n",
       "      <td>0.384135</td>\n",
       "      <td>1.238292</td>\n",
       "      <td>...</td>\n",
       "      <td>0.031637</td>\n",
       "      <td>0.410820</td>\n",
       "      <td>0.537824</td>\n",
       "      <td>0.232067</td>\n",
       "      <td>0.533684</td>\n",
       "      <td>3.945134</td>\n",
       "      <td>47</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9475</th>\n",
       "      <td>d1a6ca3</td>\n",
       "      <td>-0.017355</td>\n",
       "      <td>0.557371</td>\n",
       "      <td>-0.329742</td>\n",
       "      <td>-0.080573</td>\n",
       "      <td>0.364244</td>\n",
       "      <td>-1.530882</td>\n",
       "      <td>0.006211</td>\n",
       "      <td>0.398428</td>\n",
       "      <td>0.118003</td>\n",
       "      <td>...</td>\n",
       "      <td>0.086359</td>\n",
       "      <td>0.445390</td>\n",
       "      <td>1.640812</td>\n",
       "      <td>0.280560</td>\n",
       "      <td>0.626709</td>\n",
       "      <td>5.330647</td>\n",
       "      <td>165</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9476</th>\n",
       "      <td>d1a79a1</td>\n",
       "      <td>0.003720</td>\n",
       "      <td>0.567535</td>\n",
       "      <td>0.066963</td>\n",
       "      <td>-0.016869</td>\n",
       "      <td>0.284308</td>\n",
       "      <td>-0.303642</td>\n",
       "      <td>0.045457</td>\n",
       "      <td>0.382817</td>\n",
       "      <td>0.818232</td>\n",
       "      <td>...</td>\n",
       "      <td>0.085955</td>\n",
       "      <td>0.398191</td>\n",
       "      <td>1.547186</td>\n",
       "      <td>0.321658</td>\n",
       "      <td>0.618853</td>\n",
       "      <td>5.789847</td>\n",
       "      <td>97</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11838</th>\n",
       "      <td>g1gk9.1</td>\n",
       "      <td>0.019443</td>\n",
       "      <td>0.529738</td>\n",
       "      <td>0.388853</td>\n",
       "      <td>-0.120233</td>\n",
       "      <td>0.340746</td>\n",
       "      <td>-2.404651</td>\n",
       "      <td>0.030144</td>\n",
       "      <td>0.396650</td>\n",
       "      <td>0.602877</td>\n",
       "      <td>...</td>\n",
       "      <td>0.051418</td>\n",
       "      <td>0.434469</td>\n",
       "      <td>1.028352</td>\n",
       "      <td>0.197246</td>\n",
       "      <td>0.514560</td>\n",
       "      <td>3.944922</td>\n",
       "      <td>766</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11839</th>\n",
       "      <td>g1jjo.1</td>\n",
       "      <td>-0.006536</td>\n",
       "      <td>0.529872</td>\n",
       "      <td>-0.137259</td>\n",
       "      <td>-0.095767</td>\n",
       "      <td>0.350531</td>\n",
       "      <td>-2.011113</td>\n",
       "      <td>0.032028</td>\n",
       "      <td>0.386703</td>\n",
       "      <td>0.672583</td>\n",
       "      <td>...</td>\n",
       "      <td>0.076008</td>\n",
       "      <td>0.438206</td>\n",
       "      <td>1.596174</td>\n",
       "      <td>0.267624</td>\n",
       "      <td>0.596277</td>\n",
       "      <td>5.620098</td>\n",
       "      <td>336</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11840</th>\n",
       "      <td>g1k2x.1</td>\n",
       "      <td>-0.006536</td>\n",
       "      <td>0.529872</td>\n",
       "      <td>-0.137259</td>\n",
       "      <td>-0.095767</td>\n",
       "      <td>0.350531</td>\n",
       "      <td>-2.011113</td>\n",
       "      <td>0.032028</td>\n",
       "      <td>0.386703</td>\n",
       "      <td>0.672583</td>\n",
       "      <td>...</td>\n",
       "      <td>0.076008</td>\n",
       "      <td>0.438206</td>\n",
       "      <td>1.596174</td>\n",
       "      <td>0.267624</td>\n",
       "      <td>0.596277</td>\n",
       "      <td>5.620098</td>\n",
       "      <td>292</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11841</th>\n",
       "      <td>g1o7</td>\n",
       "      <td>-0.015721</td>\n",
       "      <td>0.541919</td>\n",
       "      <td>-0.314412</td>\n",
       "      <td>-0.112268</td>\n",
       "      <td>0.351170</td>\n",
       "      <td>-2.245357</td>\n",
       "      <td>0.013389</td>\n",
       "      <td>0.386950</td>\n",
       "      <td>0.267779</td>\n",
       "      <td>...</td>\n",
       "      <td>0.095032</td>\n",
       "      <td>0.440603</td>\n",
       "      <td>1.900635</td>\n",
       "      <td>0.247321</td>\n",
       "      <td>0.604274</td>\n",
       "      <td>4.946411</td>\n",
       "      <td>96</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11842</th>\n",
       "      <td>g1ugw.1</td>\n",
       "      <td>0.019443</td>\n",
       "      <td>0.529738</td>\n",
       "      <td>0.388853</td>\n",
       "      <td>-0.120233</td>\n",
       "      <td>0.340746</td>\n",
       "      <td>-2.404651</td>\n",
       "      <td>0.030144</td>\n",
       "      <td>0.396650</td>\n",
       "      <td>0.602877</td>\n",
       "      <td>...</td>\n",
       "      <td>0.051418</td>\n",
       "      <td>0.434469</td>\n",
       "      <td>1.028352</td>\n",
       "      <td>0.197246</td>\n",
       "      <td>0.514560</td>\n",
       "      <td>3.944922</td>\n",
       "      <td>150</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2371 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          index  w2c_words_0_mean  w2c_words_0_std  w2c_words_0_sum  \\\n",
       "9472   d1a1va1          -0.015281         0.557728        -0.290334   \n",
       "9473   d1a5ea_          -0.051581         0.527122        -0.980044   \n",
       "9474   d1a62a1          -0.018240         0.560308        -0.310081   \n",
       "9475   d1a6ca3          -0.017355         0.557371        -0.329742   \n",
       "9476   d1a79a1           0.003720         0.567535         0.066963   \n",
       "...         ...               ...              ...              ...   \n",
       "11838  g1gk9.1           0.019443         0.529738         0.388853   \n",
       "11839  g1jjo.1          -0.006536         0.529872        -0.137259   \n",
       "11840  g1k2x.1          -0.006536         0.529872        -0.137259   \n",
       "11841      g1o7         -0.015721         0.541919        -0.314412   \n",
       "11842  g1ugw.1           0.019443         0.529738         0.388853   \n",
       "\n",
       "       w2c_words_1_mean  w2c_words_1_std  w2c_words_1_sum  w2c_words_2_mean  \\\n",
       "9472          -0.053241         0.318554        -1.011584          0.052057   \n",
       "9473          -0.100268         0.368242        -1.905095          0.034407   \n",
       "9474          -0.054832         0.301117        -0.932145          0.072841   \n",
       "9475          -0.080573         0.364244        -1.530882          0.006211   \n",
       "9476          -0.016869         0.284308        -0.303642          0.045457   \n",
       "...                 ...              ...              ...               ...   \n",
       "11838         -0.120233         0.340746        -2.404651          0.030144   \n",
       "11839         -0.095767         0.350531        -2.011113          0.032028   \n",
       "11840         -0.095767         0.350531        -2.011113          0.032028   \n",
       "11841         -0.112268         0.351170        -2.245357          0.013389   \n",
       "11842         -0.120233         0.340746        -2.404651          0.030144   \n",
       "\n",
       "       w2c_words_2_std  w2c_words_2_sum  ...  w2c_words_8_mean  \\\n",
       "9472          0.373142         0.989092  ...          0.057214   \n",
       "9473          0.396551         0.653733  ...          0.132534   \n",
       "9474          0.384135         1.238292  ...          0.031637   \n",
       "9475          0.398428         0.118003  ...          0.086359   \n",
       "9476          0.382817         0.818232  ...          0.085955   \n",
       "...                ...              ...  ...               ...   \n",
       "11838         0.396650         0.602877  ...          0.051418   \n",
       "11839         0.386703         0.672583  ...          0.076008   \n",
       "11840         0.386703         0.672583  ...          0.076008   \n",
       "11841         0.386950         0.267779  ...          0.095032   \n",
       "11842         0.396650         0.602877  ...          0.051418   \n",
       "\n",
       "       w2c_words_8_std  w2c_words_8_sum  w2c_words_9_mean  w2c_words_9_std  \\\n",
       "9472          0.406746         1.087065          0.314882         0.602142   \n",
       "9473          0.420858         2.518142          0.236420         0.598728   \n",
       "9474          0.410820         0.537824          0.232067         0.533684   \n",
       "9475          0.445390         1.640812          0.280560         0.626709   \n",
       "9476          0.398191         1.547186          0.321658         0.618853   \n",
       "...                ...              ...               ...              ...   \n",
       "11838         0.434469         1.028352          0.197246         0.514560   \n",
       "11839         0.438206         1.596174          0.267624         0.596277   \n",
       "11840         0.438206         1.596174          0.267624         0.596277   \n",
       "11841         0.440603         1.900635          0.247321         0.604274   \n",
       "11842         0.434469         1.028352          0.197246         0.514560   \n",
       "\n",
       "       w2c_words_9_sum  len  id1  id2  id3  \n",
       "9472          5.982751  136  NaN  NaN  NaN  \n",
       "9473          4.491974  156  NaN  NaN  NaN  \n",
       "9474          3.945134   47  NaN  NaN  NaN  \n",
       "9475          5.330647  165  NaN  NaN  NaN  \n",
       "9476          5.789847   97  NaN  NaN  NaN  \n",
       "...                ...  ...  ...  ...  ...  \n",
       "11838         3.944922  766  NaN  NaN  NaN  \n",
       "11839         5.620098  336  NaN  NaN  NaN  \n",
       "11840         5.620098  292  NaN  NaN  NaN  \n",
       "11841         4.946411   96  NaN  NaN  NaN  \n",
       "11842         3.944922  150  NaN  NaN  NaN  \n",
       "\n",
       "[2371 rows x 35 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分离训练和测试集\n",
    "test = train[train['id1'].isnull()]\n",
    "train = train[train['id1'].notnull()]\n",
    "# train['id2'] = train['id2'].astype('int')\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d21212c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = lgb.LGBMClassifier(\n",
    "    eval_metric='auc',\n",
    "    max_depth=5,\n",
    "    learning_rate=0.08,\n",
    "    n_estimators=300,\n",
    "    gamma=0.1,\n",
    "    subsample=0.8,\n",
    "    min_child_weight=1,\n",
    "    colsample_bytree=0.8,\n",
    ")\n",
    "\n",
    "# a = [7, 13, 25, 26, 65, 66, 67, 68]\n",
    "# x_train.drop(x_train.columns[a], axis=1, inplace=True)\n",
    "# x_train, x_test, y_train, y_test = train_test_split(x_train, y_train,\n",
    "#                                                    test_size=0.2, random_state=1)\n",
    "\n",
    "for pre_fea in ['id1','id2','id3']:\n",
    "    fea = [i for i in train.columns if i not in ['id1','id2','id3','index']]\n",
    "    model.fit(train[fea], train[pre_fea])\n",
    "    test[pre_fea] = model.predict(test[fea])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5fe4739",
   "metadata": {},
   "outputs": [],
   "source": [
    "test['id4'] = test.apply(lambda x:str(x.id1)+'.'+str(x.id2),axis=1)\n",
    "test['result'] = test.apply(lambda x:x['id4'] if x['id4'] in df['id3'].unique() else x['id3'],axis=1)\n",
    "# 判断这个字母id和数字id组合起来是否出现过，如果没出现过，那么就替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b517180",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "fed16d14",
   "metadata": {},
   "outputs": [],
   "source": [
    "test['result'] = test['id3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "0da74d48",
   "metadata": {},
   "outputs": [],
   "source": [
    "write = test[['index','result']]\n",
    "write.columns=['sample_id','category_id']\n",
    "write['sample_id']=write['sample_id'].apply(lambda x:x.replace(' ',''))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0568f820",
   "metadata": {},
   "outputs": [],
   "source": [
    "write.to_csv('sub.csv',index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
